<?php

namespace Core\init;

use Core\lib\DbPool;
use Swoole\Coroutine\MySQL;

class CoMysqlPool extends DbPool
{
    public function __construct($min = 5, $max = 10)
    {
        global $GLOBALS_CONFIGS;
        if (!isset($GLOBALS_CONFIGS['db_pool']) || empty($GLOBALS_CONFIGS['db_pool'])) {
            throw new \Exception("未设置连接池配置参数");
        }
        $min = $GLOBALS_CONFIGS['min'];
        $max = $GLOBALS_CONFIGS['max'];
        $isDelTime = $GLOBALS_CONFIGS['isdeltime'];
        parent::__construct($min, $max,$isDelTime);
    }

    function newDb()
    {
        global $GLOBALS_CONFIGS;
        if (!isset($GLOBALS_CONFIGS['db']) || !isset($GLOBALS_CONFIGS['db']['default'])) {
            throw new \Exception("数据库配置异常");
        }
        $db = $GLOBALS_CONFIGS['db']['default'];
        $config = [
            'host' => $db['host'],
            'user' => $db['username'],
            'password' => $db['password'],
            'database' => $db['database'],
            'port' => $db['port'],
            'charset' => $db['charset']
        ];
        $coMysql = new MySQL();
        $coMysql->connect($config);
        return $coMysql;
    }

}
